package router

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"live-core/core"
	"live-core/logging"
)

func StreamChannelCodec(c *gin.Context) {
	if !core.StreamManager.ChannelExist(c.Param("uuid"), c.Param("channel")) {
		c.IndentedJSON(500, Message{Status: 0, Payload: core.ErrorStreamNotFound.Error()})
		logging.ZapLog.Error(core.ErrorStreamNotFound.Error(),
			zap.String("stream", c.Param("uuid")), zap.String("channel", c.Param("channel")), zap.String("call", "ChannelExist"))
		return
	}
	codecs, err := core.StreamManager.ChannelCodecs(c.Param("uuid"), c.Param("channel"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "StreamChannelCodec"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: codecs})
}

func StreamChannelInfo(c *gin.Context) {
	info, err := core.StreamManager.ChannelInfo(c.Param("uuid"), c.Param("channel"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "StreamChannelInfo"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: info})
}

func StreamChannelReload(c *gin.Context) {
	err := core.StreamManager.ChannelReload(c.Param("uuid"), c.Param("channel"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "ChannelReload"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func StreamChannelEdit(c *gin.Context) {
	var payload core.ChannelST
	err := c.BindJSON(&payload)
	if err != nil {
		c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "BindJSON"))
		return
	}
	err = core.StreamManager.ChannelEdit(c.Param("uuid"), c.Param("channel"), payload)
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "ChannelEdit"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func HTTPAPIServer(c *gin.Context) {
	err := core.StreamManager.ChannelDelete(c.Param("uuid"), c.Param("channel"))
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "ChannelDelete"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}

func StreamChannelAdd(c *gin.Context) {
	var payload core.ChannelST
	err := c.BindJSON(&payload)
	if err != nil {
		c.IndentedJSON(400, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "BindJSON"))
		return
	}
	err = core.StreamManager.ChannelAdd(c.Param("uuid"), c.Param("channel"), payload)
	if err != nil {
		c.IndentedJSON(500, Message{Status: 0, Payload: err.Error()})
		logging.ZapLog.Error(err.Error(), zap.String("stream", c.Param("uuid")),
			zap.String("channel", c.Param("channel")), zap.String("call", "ChannelAdd"))
		return
	}
	c.IndentedJSON(200, Message{Status: 1, Payload: core.Success})
}
